<?php

namespace app\controllers;

use app\models\Book;
use app\models\BookArticle;
use app\models\Log;
use yii\web\Controller;

class DefaultController extends Controller
{

    public $layout = 'test';
    public function actionIndex()
    {
        return $this->render('index');
    }
    public function actionSo()
    {
        for($i=3;$i<=67;$i++){
            $URL='http://www.biquge.info/list/4_'.$i.'.html';
            $this->Zz($URL);
        }

        //return $this->render('so');
    }
    public function Zz($URL)
    {
        //$ss='完美风暴';
        //$URL='http://www.biquge.info/list/3_3.html';
        $str=$this->vget($URL,'');
       // return $str;
        //$strZheng='/cpos="title".*?class="/si';
        $strZheng ='/<li><span class=\"s1\">.*?<\/li>/si';
        preg_match_all ( $strZheng , $str , $array );//匹配出地址和内容
        $BOOK= new Book();
        foreach($array[0] as $ks=>$kv){
            if(strlen($kv)>290){
                $strZheng1='/http:\/\/.*?\/"/si';
                $strZheng2='/\/">.*?<\/a>/si';
                $strZheng3='/">.*?]/si';
                $strZheng4='/"s4">.*?<\/span>/si';
                preg_match ( $strZheng2 , $kv , $array2 );//书名
                $array2=strtr($array2[0],['/">'=>'',"</a>"=>'']);
                $title=iconv("gbk","UTF-8", $array2);
                if($BOOK::find()->where(['title'=>$title])->one()){

                }else{
                    preg_match ( $strZheng1 , $kv , $array1 );//匹配出地址和内容
                    preg_match ( $strZheng3 , $kv , $array3 );//类别
                    preg_match ( $strZheng4 , $kv , $array4 );//作者
                    $array3=strtr($array3[0],['">['=>'',"]"=>'']);
                    $array4=strtr($array4[0],['"s4">'=>'',"</span>"=>'']);

                    $id=$this->generateuuid();
                    $ssa=array('id'=>$id,
                        'title'=>iconv("gbk","UTF-8", $array2),
                        'category'=>iconv("gbk","UTF-8",$array3),
                        'author'=>iconv("gbk","UTF-8", $array4),
                        'socure'=>substr($array1[0],0,-1),
                        'create_time'=>time());


                    $BOOK::find()->createCommand()->insert(
                        'book',$ssa
                    )->execute();
                    echo '-'.$title.'<br>';
                }
            }
        }

//                $BOOK::find()->createCommand()->insert(
//                    'book',array('id'=>$id,
//                        'title'=>$title,
//                        'category'=>$category,
//                        'author'=>trim($titlearray[$v]),
//                        'socure'=>substr($array1[0],0,-1),
//                        'create_time'=>time())
//                )->execute();

    }
    public function actionZ($ss)
    {
        //$ss='完美风暴';
        $URL='http://so.biquge.info/cse/search?q='.$ss.'&s=4403055038496925232&entry=1';
        $str=$this->vget($URL,'');
        $strZheng='/cpos="title".*?class="/si';
        $strZhengzz='/作者.*?<\/p>/si';
        preg_match_all ( $strZhengzz , $str , $array );//匹配出地址和内容
        $titlearray=array();
        foreach($array[0] as $ks=>$kv){
            $strkv='/<span>.*?<\/span>/si';
            preg_match ( $strkv , $kv , $arraytitle );
            $titlearray[]=strtr($arraytitle[0],['<span>'=>'','</span>'=>'']);
        }
       // echo $strZheng;
        preg_match_all ( $strZheng , $str , $array );//匹配出地址和内容

        $BOOK=new Book();
       // return $str;
        foreach($array[0] as $v=>$s){
            $strZheng1='/http:\/\/.*?\/"/si';
            preg_match ( $strZheng1 , $s , $array1 );//匹配出地址和内容
            $id=$this->generateuuid();
            $strZheng2='/title=".*?"/si';
            preg_match ( $strZheng2 , $s , $strZhengarr );//匹配出地址和内容
            $title=strtr($strZhengarr[0],['"'=>'','title='=>'']);
            if($BOOK::find()->where(['title'=>$title])->one()){

            }else{
                $BOOK::find()->createCommand()->insert(
                        'book',array('id'=>$id,
                        'title'=>$title,
                        'author'=>trim($titlearray[$v]),
                        'socure'=>substr($array1[0],0,-1),
                        'create_time'=>time())
                )->execute();
            }
        }
    }

    /**
     * 开始任务抓取
     * @author  LiShaoWei
     * @access  public
     * @since   1.0
     */
    public function actionSoo()
    {
       $BookModel= new Book();
        $allTitle=$BookModel::find()->select(['id','title','socure'])->where(['is_state'=>0])->orderBy('create_time desc')->limit('10')->asArray()->all();
        foreach($allTitle as $u=>$url){
            $i=$this->Cai($url['socure'],$url['id']);
            echo $url['title'].'成功解析'. $i.'条';
            $Book1Model= new Log();
            $allTitle1=$Book1Model::find()->select(['id','url'])->where(['book_id'=>$url['id'],'is_state'=>0])->asArray()->all();
            foreach($allTitle1 as $u1=>$url1){
                $i=$this->zcontent($url1['url'],$url1['id']);
                echo $url['title'].'成功抓取'. $i.'条';
                $Book1Model::find()->createCommand()->update('log',['is_state'=>1],['id'=>$url1['id']])->execute();
            }
            $BookModel::find()->createCommand()->update('book',['is_state'=>1],['id'=>$url['id']])->execute();
        }
    }
    public function actionWs()
    {
        while(1){
            $Book1Model= new Log();
            $Book1Model::find()->createCommand()->delete('log',['is_state'=>1])->execute();
        }
    }
    /**
     * 死循环爬取任务
     * @author  LiShaoWei
     * @access  public
     * @since   1.0
     */
    public function actionWh(){
        while(1){
            $ch = curl_init();
            // 2. 设置选项，包括URL
            curl_setopt($ch,CURLOPT_URL,"http://y.com/default/sooo");
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch,CURLOPT_HEADER,0);
            // 3. 执行并获取HTML文档内容
            $output = curl_exec($ch);
            if($output === FALSE ){
                echo "CURL Error:".curl_error($ch);
            }
            // 4. 释放curl句柄
            curl_close($ch);
            echo $output;
        }
    }
    public function actionSooo()
    {
        $Book1Model= new Log();
        $i=1;
        $allTitle1=$Book1Model::find()->select(['id','url','book_id'])->where(['is_state'=>0])->limit('10')->asArray()->all();
        foreach($allTitle1 as $u1=>$url1){
            $this->zcontent($url1['url'],$url1['book_id']);
            $Book1Model::find()->createCommand()->update('log',['is_state'=>1],['id'=>$url1['id']])->execute();
            $i++;
        }
        return  '成功抓取'. $i.'条<br/>';
    }
    public function generateuuid()
    {
       // $this->Cai(substr($array1[0],0,-1),$id);
        $charid = strtoupper(md5(uniqid(mt_rand(), true)));
        $hyphen = '';
        $hyphen = chr(45);
        $uuid = substr($charid, 0, 8).$hyphen
            .substr($charid, 8, 4).$hyphen
            .substr($charid,12, 4).$hyphen
            .substr($charid,16, 4).$hyphen
            .substr($charid,20,12);
        return $uuid;
    }
    public function Cai($url,$id)
    {
//        http://www.nanrenshu.com/Novel.Asp?ID=368&Y=-1
        $BookArticleModel= new BookArticle();
        //$url="http://www.biquge.info/22_22712/";
        $strIng=$this->vget($url,'');
//        $tt = '/\[{".*?"}\]/si';
        $strZheng='/<dd>.*?<\/dd>/si';
        preg_match_all ( $strZheng , $strIng , $array );//匹配出地址和内容
        $i=1;
        foreach($array[0] as $s=>$k){
            preg_match('/[0-9]{4,}\w*\.html/si',$k,$arrayUrl); //撇配地址
            if(empty($arrayUrl[0])){
                //return '成功解析'. $i.'条';
                return $i;
            }
            $rt=json_encode($arrayUrl,true);
            $urlTitle=$url.$arrayUrl[0];
            if(!$BookArticleModel::find()->where(['book_id'=>$id,'url'=>$urlTitle])->exists()){
                $BookArticleModel::find()->createCommand()->insert('log',
                    [
                        'log'=>iconv("gbk","UTF-8", $rt) ,
                        'log2'=>iconv("gbk","UTF-8", $arrayUrl[0]),
                        'book_id'=>$id,
                        'url'=>$urlTitle,
                        'is_state'=>0,
                        'create_time'=>time()
                    ])->execute();
                $i++;
            }
        }
       // echo '成功解析'. $i.'条';
        return $i;
    }


    public function zcontent($urlTitle,$id)
    {
        $BookArticleModel= new BookArticle();
        $strIngTitleContent=$this->vget($urlTitle,'');

        preg_match('/<h1>.*?<\/h1>/si',$strIngTitleContent,$arrayTitle); //匹配标题
        $title=strtr($arrayTitle[0],array('<h1>'=>'','</h1>'=>''));//去除多余
        preg_match('/id="content".*?<\/div>/si',$strIngTitleContent,$arrayTitleContent); //匹配标题
        $arrayContent=strtr($arrayTitleContent[0],array('&nbsp;'=>'','</div>'=>''));//去除多余
        $pattern = "/<.*>/";
        $pattern1="/id=.*? >/si";
        $content= preg_replace($pattern,"",$arrayContent);
        $content1= preg_replace($pattern1,"",$content);
        $title=iconv("gbk","UTF-8", $title);
        $i=0;
        if(!$BookArticleModel::find()->where(['book_id'=>$id,'title'=>$title])->exists()){
            $BookArticleModel::find()->createCommand()->insert('book_article', [
                'book_id'=>$id,
                'title'=>$title,
                'content'=>iconv( "gbk","UTF-8", $content1) ,
                'url'=>$urlTitle,
                'create_time'=>time()
            ])->execute();
            $i++;
        }
        return $i;
    }
    function vget ( $url , $ucookie )
    { // 模拟登录获取Cookie函数

        $curl = curl_init (); // 启动一个CURL会话
        curl_setopt ( $curl , CURLOPT_URL , $url ); // 要访问的地址
        curl_setopt ( $curl , CURLOPT_SSL_VERIFYPEER , 0 ); // 对认证证书来源的检查
        curl_setopt ( $curl , CURLOPT_SSL_VERIFYHOST , 2 ); // 从证书中检查SSL加密算法是否存在
        //curl_setopt ( $curl , CURLOPT_USERAGENT , $_SERVER['HTTP_USER_AGENT'] ); // 模拟用户使用的浏览器
        curl_setopt ( $curl , CURLOPT_FOLLOWLOCATION , 1 ); // 使用自动跳转
        curl_setopt ( $curl , CURLOPT_AUTOREFERER , 1 ); // 自动设置Referer
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);
        curl_setopt ( $curl , CURLOPT_COOKIEJAR , $ucookie ); // 存放Cookie信息的文件名称
        curl_setopt ( $curl , CURLOPT_COOKIEFILE , $ucookie ); // 读取上面所储存的Cookie信息
        curl_setopt ( $curl , CURLOPT_TIMEOUT , 30 ); // 设置超时限制防止死循环
        curl_setopt ( $curl , CURLOPT_HEADER , 0 ); // 显示返回的Header区域内容
        curl_setopt ( $curl , CURLOPT_RETURNTRANSFER , 1 ); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec ( $curl ); // 执行操作
        if (curl_errno ( $curl )) {
            echo 'Errno' . curl_error ( $curl );
        }
        curl_close ( $curl ); // 关闭CURL会话
        return $tmpInfo; // 返回数据
    }
}
